package org.wangduqing.ibatisdao.spring.rowMapper;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.JdbcUtils;
import org.wangduqing.ibatisdao.spring.map.IbatisDaoLinkedMap;

public class IbatisColumnMapRowMapper implements RowMapper<Map<String, Object>> {
	
	public IbatisColumnMapRowMapper(){
		
		
		
	}
	
	protected Map<String, Object> createColumnMap(int columnCount){
		
		return new IbatisDaoLinkedMap(columnCount);
		
	}
	
	protected String getColumnKey(String columnName){
		
		return columnName;
		
	}
	
	protected Object getColumnValue(ResultSet rs, int index) throws SQLException{
		
		return JdbcUtils.getResultSetValue(rs, index);
		
	}
	
	public Map<String, Object> mapRow(ResultSet resultSet, int rowNum)throws SQLException{
		
		ResultSetMetaData rsmd = resultSet.getMetaData();
		int columnCount = rsmd.getColumnCount();
		Map<String, Object> mapOfColValues = createColumnMap(columnCount);
		for(int i=0; i<=columnCount; i++){
			
			String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));
			Object obj = getColumnValue(resultSet, i);
			mapOfColValues.put(key, obj);
				
		}
		
		return mapOfColValues;
		
	}

}
